package java6699.nio2;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @program: java_zzx
 * @description:
 * @author: zzx
 * @create: 2022-10-19 16:42
 **/
public class NIO2Dir6699Controller {
    Path path;
    @FXML
    public TextField tfDir;
    @FXML
    public Label lblCount;
    @FXML
    public TableView<MyFile6699> tableFiles;
    @FXML
    public TableColumn<MyFile6699,String> colName;
    @FXML
    public TableColumn<MyFile6699,String> colTime;
    @FXML
    public TableColumn<MyFile6699,String> colType;
    @FXML
    public TableColumn<MyFile6699,String> colSize;
    @FXML
    private void initialize(){
        colName.setCellValueFactory(new PropertyValueFactory<>("name"));
        colType.setCellValueFactory(new PropertyValueFactory<>("type"));
        colTime.setCellValueFactory(new PropertyValueFactory<>("modifiedTime"));
        colSize.setCellValueFactory(new PropertyValueFactory<>("size"));
    }

    @FXML
    public void listAll(ActionEvent actionEvent) throws IOException {
        String filename = tfDir.getText().trim();
        //如果输入为空，则显示当前目录
        if (filename.isEmpty()) {
            filename = ".";
        } else {
            //检查输入的目录是否正确（Files的isDirectory方法）
            if (!Files.isDirectory(path)) {
                lblCount.setText("不是目录！");
            }else if (filename.matches("[a-zA-Z]:")){
                filename +=".";
            }
        }
        Path p1 = Paths.get(filename);
        tfDir.setText(p1.toString());
        ObservableList<MyFile6699> olist = tableFiles.getItems();
        olist.clear();
        List<MyFile6699> file6699List = new ArrayList<>();
        //使用Files的list方法得到目录中的所有Path对象
        Stream<Path> streampath = Files.list(p1);
        streampath.forEach(this::addList);

    }

    private void addList(Path path) {
        MyFile6699 file = new MyFile6699();
        file.setName(path.getFileName().toString());
        BasicFileAttributes attributes = null;
        try {
            attributes = Files.readAttributes(path,BasicFileAttributes.class);
            LocalDateTime time = attributes.lastModifiedTime().toInstant()
                    .atZone(ZoneId.systemDefault()).toLocalDateTime();
            //将LocalDateTime按要求格式化为字符串
            String strTime = time.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
            file.setModifiedTime(strTime);
            if (Files.isDirectory(path)){
                file.setSize("");
                file.setType("文件夹");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    public void doubleClick(MouseEvent mouseEvent) {
    }
}
